package controllers.power.admin;


import controllers.BaseController;
import controllers.auth.Secure;
import controllers.auth.SkipAuth;
import helper.GlobalConfig;
import models.admin.AdminBusiness;
import models.admin.AdminBusinessOperate;
import models.admin.enums.BusinessType;
import models.admin.enums.OperateType;
import models.constants.DeletedStatus;
import org.apache.commons.lang.StringUtils;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;
import util.common.ConvertUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务管理
 * Created by youliangcheng on 17/4/7.
 */
@With(BaseController.class)
public class AdminBusinessCtrl extends Controller {

    public static void loadList(Integer pageNumber,
                                Long id,
                                String code,
                                String name,
                                BusinessType businessType) {
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String, Object> searchMap = new HashMap<>();
        if (id != null)
            searchMap.put("id", id);

        if (StringUtils.isNotBlank(code))
            searchMap.put("code", "%" + code + "%");

        if (StringUtils.isNotBlank(name))
            searchMap.put("name", "%" + name + "%");

        if (businessType != null)
            searchMap.put("businessType", businessType);

        JPAExtPaginator<AdminBusiness> resultPage = AdminBusiness.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber, code, name, businessType);
    }

    //删除业务
    public static void deletedBusiness(long id, Integer pageNumber) {
        pageNumber = pageNumber == null ? 1 : pageNumber;
        AdminBusiness adminBusiness = AdminBusiness.findByBusinessId(id);
        adminBusiness.deleted = DeletedStatus.DELETED;
        Secure.setBaseInfo(adminBusiness);
        adminBusiness.save();
        flash.put("msg", "删除成功");
        redirect("/power/admin/AdminBusinessCtrl/loadList/" + pageNumber);

    }

    public static void loadDetail(long id, Integer pageNumber) {
        pageNumber = pageNumber == null ? 1 : pageNumber;
        AdminBusiness adminBusiness = new AdminBusiness();
        List<AdminBusinessOperate> operateList = new ArrayList<>();
        if (id > 0) {
            adminBusiness = AdminBusiness.findByBusinessId(id);
            operateList = AdminBusinessOperate.loadByBusinessId(id);
        }
        render(adminBusiness, operateList, id, pageNumber);
    }


    //保存业务
    public static void saveDetail(AdminBusiness adminBusiness, Integer pageNumber) {

        if (adminBusiness.id != null && adminBusiness.id > 0) {
            Secure.setBaseInfo(adminBusiness);
            adminBusiness.update();
        } else {
            //自动生成编号
            adminBusiness.loadKey();
            adminBusiness.deleted = DeletedStatus.UN_DELETED;
            Secure.setBaseInfo(adminBusiness);
            adminBusiness.save();
        }

        redirect("/power/admin/AdminBusinessCtrl/loadDetail/" + adminBusiness.id + "/" + pageNumber);


    }

    //删除业务操作
    public static void deletedBusinessOperate(long id, Integer pageNumber) {

        AdminBusinessOperate operate = AdminBusinessOperate.findById(id);
        long businessId = operate.business.id;
        operate.delete();
        flash.put("msg", "删除成功");
        redirect("/power/admin/AdminBusinessCtrl/loadDetail/" + businessId + "/" + pageNumber);

    }

    //保存业务操作
    public static void saveBusinessOperate(AdminBusinessOperate operate, Integer pageNumber) {

        if (operate.id != null && operate.id > 0) {
            Secure.setBaseInfo(operate);
            operate.update();
            businessOk("修改成功");
        } else {
            Secure.setBaseInfo(operate);
            operate.save();
            businessOk("创建成功");
        }

    }

    //保存提示
    @SkipAuth
    public static void businessOk(String msg) {
        render(msg);
    }

    //业务操作信息页面
    @SkipAuth
    public static void businessOperate(long id, Integer pageNumber, long businessId , String adminOperate) {

        AdminBusinessOperate operate = null;

        if (id > 0) {//update
            operate = AdminBusinessOperate.findById(id);
        } else if (StringUtils.isNotBlank(adminOperate)) {//添加系统操作

            String[] systems = adminOperate.split(",");
            operate = new AdminBusinessOperate();
            operate.name = systems[0];
            operate.filterKey = systems[1];
            operate.operateType = OperateType.SYSTEM;
        }

        render(operate, pageNumber, businessId);

    }

    @SkipAuth

    public static void getBusinessList() {
        List<AdminBusiness> adminBusinessList = AdminBusiness.findListByStatus();
        renderJSON(ConvertUtil.toJson(adminBusinessList));
    }

    //获取业务数据
    @SkipAuth
    public static void popBussinessList(Integer pageNumber,
                                        Long id,
                                        String code,
                                        String name){

        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String, Object> searchMap = new HashMap<>();
        if (id != null)
            searchMap.put("id", id);

        if (StringUtils.isNotBlank(code))
            searchMap.put("code", "%" + code + "%");

        if (StringUtils.isNotBlank(name))
            searchMap.put("name", "%" + name + "%");

        JPAExtPaginator<AdminBusiness> resultPage = AdminBusiness.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber);
    }

}
